package com.neymar.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neymar.entity.Recruiter;
import com.neymar.entity.User;
import com.neymar.mapper.RecruiterMapper;
import com.neymar.service.RecruiterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.neymar.util.PageUtils;
import com.neymar.vo.RecruiterVO;
import com.neymar.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 招聘者表 服务实现类
 * </p>
 *
 * @author neymar
 * @since 2021-06-02
 */
@Service
public class RecruiterServiceImpl implements RecruiterService {

    @Autowired
    private RecruiterMapper recruiterMapper;

    @Override
    public int save(Recruiter recruiter) {
        return recruiterMapper.insert(recruiter);
    }

    @Override
    public int deleteRecruiterById(String userId) {
        return recruiterMapper.deleteById(userId);
    }

    @Override
    public int updateRecruiterById(Recruiter recruiter) {
        return recruiterMapper.updateById(recruiter);
    }

    @Override
    public Recruiter findInfo(String userId) {
        QueryWrapper<Recruiter> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        return recruiterMapper.selectOne(wrapper);
    }

    @Override
    public Map findRecruiter(String userId) {
        return recruiterMapper.findRecruiter(userId);
    }

    @Override
    public List findRecruiterCompanyId(String userId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.select("recruiter_company_id as companyId");
        queryWrapper.eq("user_id", userId);
        List list = recruiterMapper.selectMaps(queryWrapper);
        return list;
    }

    @Override
    public int recruiterNumber() {
        QueryWrapper<Recruiter> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("count(*) as number");
        List<Map<String, Object>> maps = recruiterMapper.selectMaps(queryWrapper);
        return ((Number) maps.get(0).get("number")).intValue();

    }

    @Override
    public RecruiterVO findRecruiters(Integer current, Integer size) {
        // 分页查询
        IPage<Recruiter> iPage = new Page<>();
        iPage.setCurrent(current);
        iPage.setSize(size);

        iPage = recruiterMapper.selectPage(iPage, null);

        RecruiterVO recruiterVO = new RecruiterVO();
        recruiterVO.setCurrent(current);
        recruiterVO.setSize(size);
        recruiterVO.setPages(iPage.getPages());
        recruiterVO.setTotal(iPage.getTotal());
        recruiterVO.setRecruiters(iPage.getRecords());
        return recruiterVO;
    }

    @Override
    public RecruiterVO findRecruitersDetails(Integer current, Integer size) {


        // 获取记录
        List<Map<String, String>> details = recruiterMapper.findAllRecruiterDetails(current, size);
        RecruiterVO recruiterVO = new RecruiterVO();
        recruiterVO.setCurrent(current);
        recruiterVO.setSize(size);
        recruiterVO.setRecruitersDetails(details);

        Long total = (long) details.size();

        // 获取总的页数
        Long page = PageUtils.getPage(total, size);
        recruiterVO.setPages(page);
        recruiterVO.setTotal(total);

        return recruiterVO;
    }

}
